package com.ciwong.epaper.modules.epaper.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.GlideRoundTransform;
import com.ciwong.mobilelib.utils.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 添加书籍Adapter
 *
 * @author bin
 * @version ciwong v.1.0 2016/4/11
 * @since ciwong v.1.0
 */
public class AddBookAdapter extends BaseAdapter {
    /**
     * 添加类型：全部
     */
    public static final int ADD_TYPE_ALL = 0;

    /**
     * 添加类型：已添加
     */
    public static final int ADD_TYPE_YES = 1;

    /**
     * 添加类型：未添加
     */
    public static final int ADD_TYPE_NO = 2;

    /**
     * 上下文对象
     */
    private Context mContext;

    /**
     * 书籍列表集合
     */
    private List<EpaperInfo> mEpaperInfos;

    /**
     * 添加类型
     */
    private int addType;

    private List<EpaperInfo> epaperInfos;
    /**
     *
     */
    private boolean mFromAddBook = false;


    /**
     * 点击事件
     */
    private onAddClickListener mOnAddClickListener;

//    public Button firstBut;

    /**
     * 回调接口
     */
    public interface onAddClickListener {
        void onAddClick(View view, EpaperInfo epaperInfo);
    }

    public void setOnAddClickListener(onAddClickListener onAddClickListener) {
        this.mOnAddClickListener = onAddClickListener;
    }

    /**
     * 构造函数
     *
     * @param context     上下文对象
     * @param addType     添加类型
     * @param epaperInfos 书籍列表集合
     */
    public AddBookAdapter(Context context, int addType,
                          List<EpaperInfo> epaperInfos,
                          boolean fromAddBook) {
        this.mContext = context;
        this.addType = addType;
        this.mEpaperInfos = epaperInfos;
        this.mFromAddBook = fromAddBook;

    }

    @Override
    public int getCount() {
        return mEpaperInfos == null ? 0 : mEpaperInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mEpaperInfos == null ? null : mEpaperInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = View.inflate(mContext, R.layout.adapter_add_book, null);
        } else {
            v = convertView;
        }
        ImageView bookIcon = ViewHolder.get(v, R.id.item_book_icon);
        TextView bookName = ViewHolder.get(v, R.id.tv_book_name);
        TextView bookGrade = ViewHolder.get(v, R.id.item_book_grade);
        TextView serverName = ViewHolder.get(v, R.id.tv_service_name);
        ImageView imgFreeView = ViewHolder.get(v, R.id.img_item_free);
        ImageView imgKaiShiView = ViewHolder.get(v, R.id.kao_shi);
        ImageView ivArrow = ViewHolder.get(v, R.id.iv_arrow);
//        TextView tv_add_book_to_bookcase = ViewHolder.get(v, R.id.tv_add_book_to_bookcase);
//        LinearLayout ll_add_book_to_bookcase = ViewHolder.get(v, R.id.ll_add_book_to_bookcase);
        final EpaperInfo epaperInfo = (EpaperInfo) getItem(position);

        bookName.setText(epaperInfo.getProductName());

//        try {
//            ImageLoader.getInstance().displayImage(epaperInfo.getCover(),
//                    new ImageViewAware(bookIcon), EConstants.getBookCoverOption());
//        } catch (OutOfMemoryError error) {
//
//        }
        try {
            Glide.with(mContext).load(epaperInfo.getCover()).transform(new CenterCrop(mContext) ,new GlideRoundTransform(mContext,6)).into(bookIcon);
        } catch (Exception e) {

        }catch (OutOfMemoryError error){

        }

        if (!TextUtils.isEmpty(epaperInfo.getGradeName())) {
            bookGrade.setText(mContext.getString(R.string.book_grade,
		            epaperInfo.getGradeName()));
        }else{
            String grade = EConstants.GRADE_DATA.get(epaperInfo.getGrade());
            bookGrade.setText(mContext.getString(R.string.book_grade,
                    TextUtils.isEmpty(grade) ? "无" : grade));
        }


//        String intro = epaperInfo.getBookIntro();

        //书柜页面显示“添加书籍”按钮
        if (mFromAddBook) {
//            ll_add_book_to_bookcase.setVisibility(View.VISIBLE);
            serverName.setVisibility(View.VISIBLE);
        } else {
//            ll_add_book_to_bookcase.setVisibility(View.GONE);
            serverName.setVisibility(View.INVISIBLE);
        }


        if (mFromAddBook) {
            String servicesName= epaperInfo.getServerName();
            serverName.setText(mContext.getString(R.string.subordinate_service,
                    TextUtils.isEmpty(servicesName) ? "无" : servicesName));
            if(TextUtils.isEmpty(servicesName)){
                serverName.setVisibility(View.INVISIBLE);
            }else{
                if (epaperInfo.getSalesType()==0){
                    serverName.setVisibility(View.VISIBLE);
                }else{
                    serverName.setVisibility(View.INVISIBLE);
                }
            }
        } else {
            serverName.setVisibility(View.INVISIBLE);
        }
        imgFreeView.setVisibility(
                epaperInfo.getIsFree() == 1 ? View.VISIBLE : View.GONE);
        imgKaiShiView.setVisibility(
                epaperInfo.getRequired() == 1 ? View.VISIBLE : View.GONE);


/*        ll_add_book_to_bookcase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnAddClickListener.onAddClick(view, epaperInfo);
            }
        });*/
//        if (epaperInfo.isBookCase == 1) {//被用户添加到了书桌
//            ll_add_book_to_bookcase.setClickable(false);
//            tv_add_book_to_bookcase.setBackgroundResource(R.mipmap.added_to_desk);
//        } else {
//            ll_add_book_to_bookcase.setClickable(true);
//            tv_add_book_to_bookcase.setBackgroundResource(R.mipmap.add_to_desk);
//        }

        return v;
    }

    /**
     * 添加书籍列表集合
     *
     * @param epaperInfos 书籍列表集合
     */
    public void addAll(List<EpaperInfo> epaperInfos) {
        if (this.mEpaperInfos == null) {
            this.mEpaperInfos = new ArrayList<EpaperInfo>();
        }
        if (epaperInfos != null) {
            switch (addType) {
                case ADD_TYPE_ALL:
                    this.mEpaperInfos.addAll(epaperInfos);
                    break;

                case ADD_TYPE_YES:
                    for (EpaperInfo epaperInfo : epaperInfos) {
//                        if (epaperInfo.isAdded()) {
                        this.mEpaperInfos.add(epaperInfo);
//                        }
                    }
                    break;

                default:
                    for (EpaperInfo epaperInfo : epaperInfos) {
//                        if (!epaperInfo.isAdded()) {
                        this.mEpaperInfos.add(epaperInfo);
//                        }
                    }
                    break;
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 清空数据
     */
    public void clear() {
        if (mEpaperInfos != null) {
            mEpaperInfos.clear();
            //notifyDataSetChanged();
        }
    }

    /**
     * 获取书籍数据
     */
    public List<EpaperInfo> getEpaperInfos() {
        return this.mEpaperInfos;
    }

    /**
     * 添加数据
     *
     * @param epaperInfo 书籍数据集合
     */
    public void add(EpaperInfo epaperInfo) {
        if (this.mEpaperInfos != null) {
            this.mEpaperInfos.add(epaperInfo);
        }
    }

}
